package com.snts.synchronization.transaction;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import com.snts.synchronization.core.Change;
import com.snts.synchronization.core.UUID;

public class TransactionBuilder {

	private HashMap<UUID, Transaction> transactions;
	
	public List<Transaction> buildTransactions(List<Change> changes){
		transactions = new HashMap<UUID, Transaction>();
		for(Change ch : changes){
			Transaction transaction = transactions.get(ch.getTransactionId());
			if(transaction == null){
				transaction = new Transaction(ch.getTransactionId());
				transactions.put(transaction.getId(), transaction);
			}
			transaction.addChange(ch);
		}
		ArrayList<Transaction> trans = new ArrayList<Transaction>(transactions.values());
		Collections.sort(trans);
		return trans;
	}

}
